//Component Object
const app = getApp();
const {apiInstance} = app

Component({
  properties: {
    data: {
      type: Object,
      value: {},
      observer: function (e) {

        
        if (e) {
         console.log('shenBao',e.shenBao)
          this.setData({
            form: e.shenBao,
            projectNatureCurrent:e.shenBao?.projectNature,
            'form.startDate':e.shenBao?.constructionPeriod&&e.shenBao?.constructionPeriod[0],
            'form.endDate':e.shenBao?.constructionPeriod&&e.shenBao?.constructionPeriod[1],
            'form.deptName':this.data?.dePartList.find(item=>item.id == e.shenBao?.leadDept)?.deptName,
            'form.leadDeptName': this.data?.dePartList.find(item=>item.id == e.shenBao?.leadDept)?.deptName,
            //'form.projectTracer':e.shenBao?.projectTracer&&e.shenBao?.projectTracer?.map(item=>this.data?.dePartList.find(subItem=> subItem.id== item)?.deptName),
            'form.checkDepartName':e.shenBao?.checkDepart&&e.shenBao?.checkDepart?.map(item=>this.data?.dePartList.find(subItem=> subItem.id== item)?.deptName),
            projectContructionTypeCurrent:e.shenBao?.projectConstructionType,
            contentTypeCurrent:e.shenBao?.contentType,
            

          })
        }
      }
    },
    isEdit: {
      type: Boolean,
      value: false,
      observer: function (e) {
        if (e) {

          console.log('jibenshenbao isedit',e);

          this.setData({
            isEdit: e,
          })
        }
      }
    }
  },
  data: {
    form: {
      leadDept:[],
      leadDept_id:[],
      checkDepart:[],
      constructionPeriod:[],
      projectName: '',
      leadDept: '',
      projectLocation: '',
      projectTracer: '',
      contentType: [],
      contentTypeDesc: '',
      projectNature: [],
      floorage: '',
      designUnit: '',

      projectConstructionType: [],
      designPaperNum: '',
      designSchemeNum: '',
      examineOther: '',
      examineTotal: '',
      fireOverview: '',
      examineData: '',
      projectDesc: '',
      
    },
    isEdit:false,
    contentTypeCurrent:[],
    projectNatureCurrent:[],
    projectContructionTypeCurrent:[],
    contentTypeList: ['建筑', '工艺变更','设备安装、改造'],
    projectNatureList: ['新建', '扩建', '改造'],
    projectContructionTypeList:['外包','公司自办'],

      staffList:[],
      projectTracerShow:false,

      //部门选择
      deptBigSearchShow:false,
      dePartList:[],
      checkDepartBigSearchShow:false,


  },
  methods: {


    bindValue(e){

      this.setData({
        [e.target.dataset.itemname]:e.detail.detail.value,
      });

      console.log('e',e);
    },

    handleContentTypeChange({detail}){
      this.data.contentTypeCurrent??=[];
      const index = this.data.contentTypeCurrent?.indexOf(detail.value);
      (index == -1||index=='undefined') ? this.data.contentTypeCurrent.push(detail.value) : this.data.contentTypeCurrent.splice(index, 1);
        this.setData({
          contentTypeCurrent: this.data.contentTypeCurrent,
          'form.contentType': this.data.contentTypeCurrent
        });
    },

    handleprojectNatureChange({detail}){

      this.data.projectNatureCurrent??=[];
      const index = this.data.projectNatureCurrent?.indexOf(detail.value);
        (index == -1||index=='undefined') ? this.data.projectNatureCurrent.push(detail.value) : this.data.projectNatureCurrent.splice(index, 1);

        this.setData({
          projectNatureCurrent: this.data.projectNatureCurrent,
          'form.projectNature': this.data.projectNatureCurrent
        });
    },

    handleProjectContructionTypeChange({detail}){


      this.data.projectContructionTypeCurrent??=[];
      console.log('this.data.projectContructionTypeCurrent',this.data.projectContructionTypeCurrent);
      const index = this.data.projectContructionTypeCurrent?.indexOf(detail.value);
      (index == -1||index=='undefined') ? this.data.projectContructionTypeCurrent.push(detail.value) : this.data.projectContructionTypeCurrent.splice(index, 1);
        this.setData({
          projectContructionTypeCurrent: this.data.projectContructionTypeCurrent,
          'form.projectConstructionType': this.data.projectContructionTypeCurrent
        });
    },

    doDeptBigSearchShow(e){
      this.setData({
        deptBigSearchShow:true,

      })
    },

    doCheckDepartBigSearchShow(e){
      this.setData({
        checkDepartBigSearchShow:true,

      })
    },

    checkDepartSearchSelect(e){
      this.setData({
        ['form.checkDepart']:e.detail.map(item=>item.id),
         "form.checkDepartName":e.detail.map(item=>item.deptName),
      })
    },

    startDateChange(e){
      console.log('e',e);
      this.setData({
        ['form.startDate']:e.detail.value,
        ["form.constructionPeriod"]:[e.detail.value,this.data?.form?.constructionPeriod&&this.data?.form?.constructionPeriod[1]],
      })
    },

    endDateChange(e){
      this.setData({
        ['form.endDate']:e.detail.value,
        ["form.constructionPeriod"]:[this.data?.form?.constructionPeriod&&this.data?.form?.constructionPeriod[0]||'',e.detail.value],
      })
    },

    deptSearchSelect(e){
      this.setData({
        ['form.leadDept']:e.detail.id,
        ['form.leadDeptName']:e.detail.deptName,

      })
    },

    projectTracerSearchSelect(e){
      this.setData({
        ['form.projectTracer']:e.detail.staffName,
        ['form.projectTracerSapId']:e.detail.sapId
      })
    },
    doProjectTracerShow(){
      this.setData({
        projectTracerShow:true
      })

    },

    examineDataImgUpload(e){
      this.setData({
        ['form.uploadFileSongShen']:e.detail
      })
    
    },

    async bigSearchSearchTap(e){
      const res = await apiInstance.get(`/staffInfo/wx/getStaffByName`,{name:e.detail});
      
      this.setData({
        staffList:res.data.slice(0,50)

      })
    }
  },
  lifetimes: {
    created: async function () {
      const res = await apiInstance.get(`/v1/openApi/employee/search/departList`);
      this.setData({
        dePartList:res.data
      })

     
    },
    attached: function () {

    },
    ready: async function () {
     

    },
    moved: function () {

    },
    detached: function () {

    }
  }

});